const { adminsLogin, salesMenLogin, validate } = require('../dao/loginDao')
const jwt  =require('jsonwebtoken')
// 管理员登录
module.exports.adminsLogin = async params=> {
    // const {account,password} = params
    const data = await adminsLogin(params);
    console.log(data);
    if (data.length > 0) {
        const token = jwt.sign(
            { _id:data[0]._id, account:data[0].account}, //保存当前用户信息
            'hello',
            { expiresIn: '1h' },
        )
        return {
            message: '登录成功',
            token,
            data,
            status: 1
        };
    } else {
        return {
            message: '登录失败,账号不存在',
            status: 0
        }
    }   
}

// 管理员登录验证
module.exports.validate = async function (tata) {
    const data = await validate();
    const params = await data.find()
    if (params.length > 0) {
        const status = params.some(item => item.state == false&&item.account==tata.val)
        if(status){
            return {
                message: '该账户正常',
                status: 1
            }
        }else{
            return {
                message: '该账户已禁用或无该账号',
                status: 0
            }
        }
    }
}

// 销售员登录
module.exports.salesMenLogin = async function (params) {
    const data = await salesMenLogin(params)
    console.log(data, 22222);
    if (data.length>0) {
        const token = jwt.sign(
            {_id: data[0]._id, account: data[0].account},
            'hello',
            { expiresIn: 60 * 60 },
        )
        return {
            message: '登录成功',
            status: 1,
            token,
            params
        }
    } else {
        return {
            message: '用户名或密码错误',
            status: 0,
        }
    }
}